{-# LANGUAGE ScopedTypeVariables #-}
module Main where

import Control.Applicative
import Control.Exception
import Control.Monad
import Data.List
import Data.List.Split
import Data.Maybe
import Prelude hiding (catch)
import System.Directory
import System.Environment
import System.FilePath

main :: IO ()
main = do
    paths <- splitOn ":" <$> getEnv "PATH"
    fns   <- sort . catMaybes . concat <$> forM paths (\path -> do
                 fns <- catch (getDirectoryContents path)
                              (\(_ :: IOError) -> return [])
                 forM fns $ \fn -> do
                     fileExists <- doesFileExist (path </> fn)
                     if fileExists then return (Just fn) else return Nothing)
    mapM_ putStrLn fns

